import type { CodeMateField, CodeMateFieldType, CodeMateProject } from '@/apis/code/codeMateApi'
import type { DataField } from '@/components/DataField'
import { CodeMateMethodTypes } from '@/apis/code/codeMateApi'

export const codeProjectFields: DataField[] = [
  { name: 'projectName', title: '项目名称', require: true, maxLength: 64, tip: '必须是小写字母，数字，横杠(-)组成，不能以数字开始，例:fa4j-system', pattern: '^[a-z][a-z0-9]*(?:-[a-z][a-z0-9]*)*$', edit: data => data.id === undefined, change: projectNameChange },
  { name: 'remark', title: '项目说明', require: true, maxLength: 128 },
  { name: 'basePackage', title: '项目包名', require: true, maxLength: 128, tip: '必须是小写字母，数字，点(.)组成，不能以数字开始，例:com.fa4j.system', pattern: '^[a-z][a-z0-9]*(?:\.[a-z][a-z0-9]*)*$' },
  { name: 'moduleName', title: '模块名称', require: true, maxLength: 64, tip: '必须是小写字母，数字，点(.)组成，不能已数字开始，例:system', pattern: '^[a-z][a-z0-9]*(?:\.[a-z][a-z0-9]*)*$' },
  { name: 'moduleCode', title: '模块编码', type: 'number', require: true, tip: '必须是数字，例:9001' },
]

function projectNameChange(projectName: string, data: CodeMateProject) {
  const splits = projectName.split('-').filter(s => s !== undefined && s.length > 0)
  if (splits.length >= 2) {
    data.basePackage = `com.${splits[0]}`
    data.moduleName = splits.slice(1).join('.')
  }
}

export const codeModuleFields: DataField[] = [
  { name: 'id', title: 'ID', width: 80, columnType: 'KEY', edit: false, sort: true },
  { name: 'moduleName', title: '模块名称', require: true, maxLength: 64, width: 100, columnType: 'MAIN', edit: true, pattern: '^[a-z][a-z0-9]*(?:\.[a-z][a-z0-9]*)*$' },
  { name: 'remark', title: '模块说明', require: true, maxLength: 128, width: 180, edit: true },
  { name: 'moduleCode', title: '模块编码', type: 'number', require: true, width: 100, edit: true },
]

export const codeModelFields: DataField[] = [
  { name: 'id', title: 'ID', width: 80, columnType: 'KEY', edit: false, sort: true },
  { name: 'type', title: '模型类型', require: true, dict: 'ENUM_CODE_MATE_MODEL_TYPE', width: 100, columnType: 'MAIN', edit: data => data.id === undefined, align: 'center' },
  { name: 'name', title: '模型名称', require: true, maxLength: 64, width: 100, columnType: 'MAIN', edit: true },
  { name: 'remark', title: '模型说明', require: true, maxLength: 128, width: 180, edit: true },
  { name: 'tableName', title: '数据库表名', require: false, maxLength: 64, width: 180, show: data => data.type === 'ENTITY', edit: data => data.type === 'ENTITY' },
  { name: 'priority', title: '优先级', require: true, type: 'number', minValue: 1, maxValue: 99, width: 120, edit: true, editType: 'number', align: 'center' },
]
function methodTypeChange(value: string, row: any) {
  for (const methodType of CodeMateMethodTypes) {
    if (methodType.value === value) {
      row.name = methodType.name
      row.remark = methodType.remark
      break
    }
  }
}
export const codeMethodFields: DataField[] = [
  { name: 'id', title: 'ID', width: 80, columnType: 'KEY', edit: false, sort: true },
  { name: 'type', title: '方法类型', width: 100, edit: data => data.id === undefined, columnType: 'MAIN', require: true, dict: 'ENUM_CODE_MATE_METHOD_TYPE', align: 'center', change: methodTypeChange },
  { name: 'name', title: '方法名称', width: 100, edit: true, columnType: 'MAIN', require: true, maxLength: 64 },
  { name: 'remark', title: '方法说明', require: true, maxLength: 128, width: 180, edit: true },
  { name: 'priority', title: '优先级', require: true, type: 'number', minValue: 1, maxValue: 99, width: 120, edit: true, editType: 'number', align: 'center' },
]

function fieldTypeChange(fieldType: CodeMateFieldType, row: Partial<CodeMateField>) {
  row.length = fieldType === 'STRING' ? 32 : undefined
  row.pattern = undefined
  row.queryType = undefined
  row.defaultValue = undefined
}

export const codeEntityFieldFields: DataField[] = [
  { name: 'id', title: 'ID', width: 60, columnType: 'KEY', edit: false, sort: true },
  { name: 'type', title: '字段类型', require: true, dict: 'ENUM_CODE_MATE_FIELD_TYPE', width: 100, edit: true, columnType: 'MAIN', change: fieldTypeChange },
  { name: 'name', title: '字段名称', require: true, maxLength: 64, width: 100, edit: true, columnType: 'MAIN', align: 'center' },

  { name: 'remark', title: '参数说明', require: true, maxLength: 128, width: 120, edit: true },
  { name: 'priority', title: '优先级', require: true, type: 'number', minValue: 1, maxValue: 99, width: 120, edit: true, editType: 'number', align: 'center' },
  { name: 'required', title: '必填', width: 60, edit: true, type: 'boolean', align: 'center' },
  { name: 'queryType', title: '查询条件', width: 100, edit: true, dict: 'ENUM_QUERY_TYPE', align: 'center' },
  { name: 'length', title: '长度', width: 120, require: true, edit: data => data.type === 'STRING', type: 'number', editType: 'number', minValue: 1, align: 'center' },
  { name: 'defaultValue', title: '默认值', width: 100, edit: data => data.type === 'STRING' },
  { name: 'pattern', title: '正则验证', width: 120, edit: data => data.type === 'STRING' },
]

export const codeDictFieldFields: DataField[] = [
  { name: 'id', title: 'ID', width: 60, columnType: 'KEY', edit: false, sort: true },
  { name: 'name', title: `字典项名称`, width: 100, edit: true, columnType: 'MAIN', require: true, maxLength: 64 },
  { name: 'remark', title: '字典项说明', require: true, maxLength: 128, width: 120, edit: true },
  { name: 'priority', title: '字典项编码', require: true, type: 'number', minValue: 1, maxValue: 99, width: 120, edit: true, editType: 'number', align: 'center' },
]
